package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.Result;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/page")
    public Result getByPage(Integer page, Integer pageSize, String name) {

        Page<Dish> dishPage = new Page<>(page, pageSize);

        Page<DishDto> dishDtoPage = new Page<>();

        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();

        dishLambdaQueryWrapper.eq(name != null, Dish::getName, name);

        dishService.page(dishPage, dishLambdaQueryWrapper);

        dishDtoPage = mapperFacade.map(dishPage,/*Page.class*/dishDtoPage.getClass());

        List<Dish> records = dishPage.getRecords();

        List<DishDto> dishDtos = records.stream().map(item -> {
            DishDto dishDto = mapperFacade.map(item, DishDto.class);
            Long id = item.getCategoryId();
            Category category = categoryService.getById(id);
            if (category != null) {
                String name1 = category.getName();
                dishDto.setCategoryName(name1);
            }
            return dishDto;
        }).collect(Collectors.toList());

        dishDtoPage.setRecords(dishDtos);

        return Result.success(dishDtoPage);
    }

    @PostMapping
    public Result<String> save(@RequestBody DishDto dishDto) {

        dishService.saveWithFlavor(dishDto);

        return Result.success("添加成功");
    }


    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){

        DishDto dishDto = dishService.getByIdWithFlavor(id);

        return Result.success(dishDto);

    }

    @PutMapping
    public Result<String> update(@RequestBody DishDto dishDto){

        dishService.updateWithFlavor(dishDto);

        return Result.success("修改成功");

    }

/*    @GetMapping("/list")
    public Result list(Dish dish){

        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());

        wrapper.orderByDesc(Dish::getSort).orderByAsc(Dish::getCreateTime);


        List<Dish> dishList = dishService.list(wrapper);

        return Result.success(dishList);

    }*/


    @GetMapping("/list")
    public Result list(Dish dish){

        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());

        wrapper.orderByDesc(Dish::getSort).orderByAsc(Dish::getCreateTime);

        List<Dish> dishList = dishService.list(wrapper);

        List<DishDto> dishDtos = dishList.stream().map(item -> {
            DishDto dishDto = mapperFacade.map(item, DishDto.class);
            Long id = item.getCategoryId();
            Category category = categoryService.getById(id);
            if (category != null) {
                String name1 = category.getName();
                dishDto.setCategoryName(name1);
            }
            Long dishId = item.getId();
            LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DishFlavor::getDishId,dishId);
            List<DishFlavor> list = dishFlavorService.list(queryWrapper);
            dishDto.setFlavors(list);
            return dishDto;
        }).collect(Collectors.toList());

        return Result.success(dishDtos);

    }

    @DeleteMapping
    public Result deleteByIds(@RequestParam List<Long> ids){

        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();

        wrapper.in(Dish::getId,ids).eq(Dish::getStatus,1);

        int count = dishService.count(wrapper);

        if (count>0)
            throw new CustomException("有菜品还在售卖，无法批量删除");

        dishService.removeByIds(ids);

        return Result.success("删除成功");

    }


    @PostMapping("/status/{sta}")
    public Result updateStatus(@PathVariable("sta") String sta,@RequestParam(name = "ids") List<Long> ids){

        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();

        wrapper.in(Dish::getId,ids);

        List<Dish> list = dishService.list(wrapper);

        list  = list.stream().map(item -> {
            item.setStatus(Integer.parseInt(sta));
            return item;
        }).collect(Collectors.toList());

        dishService.updateBatchById(list);

        return  Result.success("修改成功");

    }
}
